require("../../assets/js/commonCss"); //导入公共样式
require("./course-detail.less"); //引入自己的样式

// 导入axios
const axios = require("../../utils/request");
// 设置服务器地址
axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";
// 导入weui
require("weui"); //导入weui的css
let weui = require("weui.js"); //导入weui的js
// 返回按钮
document.querySelector("#backBtn").addEventListener("click", function() {
    // alert(111)
    history.back();
});


// 接收到参数
const courseId = location.search.split('=')[1]
console.log('courseId', courseId)

// 从后端获取数据  请求接口
axios.get("/api/train/courseDetail", {
    params: {
        id: courseId,
        name: '小橘猫',
    },
}).then(res => {
    console.log(res.data);
    if (res.data.errno === 0) {
        //获取数据
        let data = res.data.data
        console.log(data);

        // 视频存储到会话存储
        sessionStorage.setItem("fragments", JSON.stringify(data.fragments))
            //渲染视频
        document.querySelector("#video").src = axios.defaults.baseURL + data.fragments[0].videoUrl;
        // 名字
        document.querySelector("#name").textContent = data.name
            // 头像
        document.querySelector("#imgurl").src = axios.defaults.baseURL + data.imgurl
            // 卡路里
        document.querySelector("#calorie").textContent = data.calorie
            //时间
        document.querySelector("#time").textContent = data.time
            // 人数
        document.querySelector("#peoplenum").textContent = data.peoplenum
            //课程描述
        document.querySelector("#desc").textContent = data.desc
            //频率
        document.querySelector("#frequency").textContent = data.frequency
            // 器械
        document.querySelector("#instrument").textContent = data.instrument

        localStorage.setItem('fragments', JSON.stringify(data.fragments))
    } else if (res.data.errno === 10003) {
        weui.alert("请先登录", function() {
            location.href = "./login.html";
        });
    }
});

// 获取节点
let play = document.querySelector("#play")
let suspend = document.querySelector("#suspend")
let video = document.querySelector("#video")
    //视频预览
play.addEventListener("click", function() {
    play.style.display = "none"
    suspend.style.display = "block"
    video.play()
})

//视频暂停
suspend.addEventListener("click", function() {
    play.style.display = "block"
    suspend.style.display = "none"
    video.pause()
})

video.addEventListener("ended", function() {
    play.style.display = "block"
    suspend.style.display = "none"
})